<template>
	<view class="container">
		<view v-for="(node, index) in nodeList" :key='index'>
			<!-- {{node.componentModel.name}} -->
			<!-- 普通导航 -->
			<jc-nav-bar v-if="node.componentModel.name=='navBar'" :nodeModel='node' @itemHandle='itemHandle'></jc-nav-bar>
			<!-- 定位导航 -->
			<jc-nav-bar-location v-if="node.componentModel.name=='navBar-location'" :ref="'jcHomeNavBar' + String(node.id)" :nodeModel='node' @itemHandle='itemHandle'></jc-nav-bar-location>
			<!-- 搜索框 -->
			<jc-search-bar v-if="node.componentModel.name=='searchBar'" :nodeModel='node' @itemHandle='itemHandle' @search='searchAction'></jc-search-bar>
			<!-- 普通文本 -->
			<jc-text v-if="node.componentModel.name=='text'" :nodeModel='node' @itemHandle='itemHandle'></jc-text>
			<!-- 富文本 -->
			<jc-richtext v-if="node.componentModel.name=='richtext'" :nodeModel='node' @itemHandle='itemHandle'></jc-richtext>
			<!-- 轮播图 -->
			<jc-swiper v-if="node.componentModel.name=='banner'" :nodeModel='node' @itemHandle='itemHandle'></jc-swiper>
			<!-- 图文九宫格 -->
			<jc-grid v-if="node.componentModel.name=='grid'" :nodeModel='node' @itemHandle='itemHandle'></jc-grid>
			<!-- 通知栏 -->
			<jc-noticebar v-if="node.componentModel.name=='noticebar'" :nodeModel='node' @itemHandle='itemHandle'
				:noticeText='noticeText'></jc-noticebar>
			<!-- 商家入驻 -->
			<jc-shopCheckin v-if="node.componentModel.name=='shopCheckin'" :ref="'shopCheckin' + String(node.id)" :nodeModel='node' @itemHandle='itemHandle' :current='currentForShopTabs' :shopCount="clientInfo.shopCount"></jc-shopCheckin>
			<!-- 商家分页展示 -->
			<jc-shopTabs v-if="node.componentModel.name=='shopTabs'" :nodeModel='node' :shopList='shopList' @tabsChange='tabsChange'></jc-shopTabs>
			<!-- 产品展示 -->
			<jc-itemCate v-if="node.componentModel.name=='itemCate'" :ref="'jcItemCate' + String(node.id)" :nodeModel='node' @itemHandle='itemHandle'></jc-itemCate>
			<!-- 标题 -->
			<jc-title v-if="node.componentModel.name=='title'" :ref="'title' + String(node.id)" :nodeModel='node' @itemHandle='itemHandle'></jc-title>
			<!-- 地图 -->
			<jc-map v-if="node.componentModel.name=='map'" :nodeModel='node' @itemHandle='itemHandle'></jc-map>
			<!-- 文章 -->
			<jc-article v-if="node.componentModel.name=='article'" :ref="'article' + String(node.id)" :nodeModel='node' @itemHandle='itemHandle'></jc-article>
			<!-- 商品 -->
			<jc-itemList v-if="node.componentModel.name=='itemList'" :ref="'itemList' + String(node.id)" :nodeModel='node' @itemHandle='itemHandle'></jc-itemList>
		</view>
	</view>
</template>

<script>
	import {
		page
	} from '@/api/page.js'
	import {tools} from '@/common/utils/tools.js'
	import {
		getLocation
	} from '@/common/utils/getLocation.js'
	export default{
		name: 'jc-container',
		props: {
			itemFilterModel: {
				type: Object,
				default(){return {}}
			},
			pageName: {
				type: [String, Number],
				default: ''
			},
		},
		data() {
			return {
				page: 1,
				size: 12,
				itemList: [],
				pageConfigId: '',
				pageConfig: {},
				nodeList: [],
				itemListType: '',
				itemListNode: [],
				itemModel: {},
				isEmpty: true
			}
		},
		mounted() {
		},
		methods:{
			loadData(){
				this.getPageByClientname()
			},
			show(){
				
			},
			pullDownRefresh(){
				this.getPageByClientname()
			},
			reachBottom() {
				for (var node of this.nodeList) {
					if (node.componentModel.name == 'article') {
						if(this.$refs['article' + String(node.id)]){
							this.$refs['article' + String(node.id)][0].reachBottom()
						}
					}
					if (node.componentModel.name == 'itemList') {
						if(this.$refs['itemList' + String(node.id)]){
							this.$refs['itemList' + String(node.id)][0].reachBottom()
						}
					}
				}
			},
			searchAction(val){
				this.$Router.push({
					name: 'itemList',
					params: {
						itemName: val
					}
				})
			},
			itemHandle(item) {
				console.log('点击事件', item)
				var query = {}
				if (item.query) {
					query = JSON.parse(item.query)
				}
				switch (query.modal) {
					case 'push':
						this.$Router.push({
							name: query.routeName,
							params: query
						})
						break;
					case 'pushTab':
						this.$Router.pushTab({
							name: query.routeName,
							params: query
						})
						break;
					default:
						break;
				}
			},
			getPageByClientname() {
				var params = {}
				if(this.pageName == 'custom'){
					params.pageConfigId = this.pageConfigId
				}else{
					params.name = this.pageName
				}
				page.getPageByClientname(params).then(res => {
					uni.stopPullDownRefresh()
					if (res.data.code == 10000) {
						this.pageConfig = res.data.data
						this.$emit('setPageConfig', this.pageConfig )
						// 加载节点
						this.nodeList = res.data.data.nodeList
						for (var node of this.nodeList) {
							if (node.componentModel.name == 'itemList') {
								if(this.$refs['itemList' + String(node.id)]){
									this.$refs['itemList' + String(node.id)][0].loadData()
								}
							} else if (node.componentModel.name == 'noticebar') {
								if (node.options1 == 'dynamic') {
									this.getHomeNoticeText(node.query, node)
								}
							}else if (node.componentModel.name == 'navBar-location') {
								// console.log('111', 'jcHomeNavBar' + String(node.id), this.$refs['jcHomeNavBar' + String(node.id)])
								this.getLocation()
							}else if (node.componentModel.name == 'shopTabs') {
								this.tabsForShop = node.advertList
								// this.tabsChange(this.currentForShopTabs)
							}else if (node.componentModel.name == 'title') {
								// this.$refs['title' + String(node.id)].loadData()
								if(this.$refs['title' + String(node.id)]){
									this.$refs['title' + String(node.id)][0].loadData()
								}
							}else if (node.componentModel.name == 'shopCheckin') {
								if(this.$refs['shopCheckin' + String(node.id)]){
									this.$refs['shopCheckin' + String(node.id)][0].loadData()
								}
							}else if (node.componentModel.name == 'article') {
								if(this.$refs['article' + String(node.id)]){
									this.$refs['article' + String(node.id)][0].loadData()
								}
							}
						}
					}
				})
			},
			
			getLocation() {
				getLocation().then(res => {
					console.log('定位返回', res)
					if(res.code == '200'){
						var data = res.data || {}
						var address = data.formatted_addresses.recommend
						var currentLocation = {}
						currentLocation.address = address
						currentLocation.latitude = data.location.lat
						currentLocation.longitude = data.location.lng
						this.$store.commit('currentLocation', currentLocation)
					}else{
						var currentLocation = {}
						currentLocation.address = '全国'
						currentLocation.latitude = 0
						currentLocation.longitude = 0
						this.$store.commit('currentLocation', currentLocation)
					}
					
					// this.currentAddr.address = address
				})
			},
		}
	}
</script>

<style>
</style>
